package edu.stu.cs.software.family.admin.service.impl;

import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.ExampleMatcher.GenericPropertyMatchers;
import org.springframework.data.domain.ExampleMatcher.StringMatcher;
import org.springframework.stereotype.Component;

import edu.stu.cs.software.family.admin.dao.UserDao;
import edu.stu.cs.software.family.admin.domain.User;
import edu.stu.cs.software.family.admin.service.UserManager;
import edu.stu.cs.software.family.base.service.impl.GenericManagerImpl;
/**
 * The interface base class inherits the GenericManagerImpl class
 * @author Administrator
 *
 */
@Component
@Transactional
public class UserManagerImpl extends GenericManagerImpl<User, Long> implements UserManager {
	
	UserDao userDao;
	
	@Override
	public List<User> findByPhone(String phone) {
		User queryObject =new User();
		queryObject.setPhone(phone);
		ExampleMatcher matcher =ExampleMatcher.matching()
				.withStringMatcher(StringMatcher.CONTAINING)
				.withIgnoreCase(true)
				.withMatcher("phone", GenericPropertyMatchers.startsWith());
		Example<User> ex = Example.of(queryObject, matcher);
		List<User> result =this.userDao.findAll(ex);
		return result;
	}

	@Override
	public List<User> findByUserName(String userName) {
		User queryObject =new User();
		queryObject.setUserName(userName);
		ExampleMatcher matcher =ExampleMatcher.matching()
				.withStringMatcher(StringMatcher.CONTAINING)
				.withMatcher("userName", GenericPropertyMatchers.startsWith())
				.withIgnorePaths("dateCreated", "dateModified");
		Example<User> ex = Example.of(queryObject, matcher);
		List<User> result =this.userDao.findAll(ex);
		return result;
	}

	@Autowired
	public void setUserDao(UserDao userDao) {
		this.userDao=userDao;
		this.dao = this.userDao;
	}
	@Override
    public User findByName(String username) {
        return this.userDao.findByUserName(username);
    }
}
